import com.aliyun.oss.OSSClient
import com.aliyun.oss.common.auth.DefaultCredentialProvider
import com.aliyun.oss.common.utils.BinaryUtil
import com.aliyun.oss.model.MatchMode
import com.aliyun.oss.model.PolicyConditions
import java.sql.Date
import java.util.*

class OssTest {
}

fun main() {
    val accessId = "9hwWaqijK8gFN6gQ" // 请填写您的AccessKeyId。
    val accessKey = "gO3qp7EOXm0s92qy4j3rDyW2ja2BTK" // 请填写您的AccessKeySecret。
    val endpoint = "oss-cn-shanghai.aliyuncs.com" // 请填写您的 endpoint。
    val bucket = "zy46" // 请填写您的 bucketname 。
    val host = "http://$bucket.$endpoint" // host的格式为 bucketname.endpoint
    val dir = "mall/"

    val credentialProvider = DefaultCredentialProvider(accessId, accessKey)
    val client = OSSClient(endpoint, credentialProvider, null)
    val expireTime: Long = 30
    val expireEndTime = System.currentTimeMillis() + expireTime * 1000
    val expiration = Date(expireEndTime)
    val policyConds = PolicyConditions()
    policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000)
    policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir)

    val postPolicy: String = client.generatePostPolicy(expiration, policyConds)
    val binaryData = postPolicy.toByteArray(charset("utf-8"))
    val encodedPolicy = BinaryUtil.toBase64String(binaryData)
    val postSignature = client.calculatePostSignature(postPolicy)

    val respMap: MutableMap<String, String> = LinkedHashMap()
    respMap["accessid"] = accessId
    respMap["policy"] = encodedPolicy
    respMap["signature"] = postSignature
    respMap["dir"] = dir
    respMap["host"] = host
    respMap["expire"] = (expireEndTime / 1000).toString()

    println(respMap)
}